
// 计算2个日期之间的间隔
export const calcTimeDifference = (d1, d2) => {
  console.log('d1=', d1.getFullYear(), d1.getMonth(), d1.getDate());
  console.log('d2=', d2.getFullYear(), d2.getMonth(), d2.getDate());
	
	// 新的实现方案开始
	const startDate = d1;
	const endDate = d2;
	let startYear = startDate.getFullYear();
	let endYear = endDate.getFullYear();
	let startMonth = startDate.getMonth();
	let endMonth = endDate.getMonth();
	let startDay = startDate.getDate();
	let endDay = endDate.getDate();
	let startHour = startDate.getHours();
	let endHour = endDate.getHours();
	let startMinute = startDate.getMinutes();
	let endMinute = endDate.getMinutes();
	let startSecond = startDate.getSeconds();
	let endSecond = endDate.getSeconds();

	let years = endYear - startYear;
	let months = endMonth - startMonth;
	let days = endDay - startDay;
	let hours = endHour - startHour;
	let minutes = endMinute - startMinute;
	let seconds = endSecond - startSecond;

	// 调整负值
	if (seconds < 0) {
			minutes -= 1;
			seconds += 60;
	}
	if (minutes < 0) {
			hours -= 1;
			minutes += 60;
	}
	if (hours < 0) {
			days -= 1;
			hours += 24;
	}
	if (days < 0) {
			// 向前借一个月，并加上该月的天数
			const lastMonth = new Date(endYear, endMonth, 0);
			days += lastMonth.getDate();
			months -= 1;
	}
	if (months < 0) {
			years -= 1;
			months += 12;
	}
	// console.log(years, months, days, hours, minutes, seconds)
	return {
	  yCount: years,
	  mCount: months,
	  dCount: days,
	  hCount: hours,
	  fCount: minutes,
	  sCount: seconds
	}
	// 新的实现方案结束

  const DayMillisecond = 24 * 60 * 60 * 1000;
  const sd = d1, ed = d2;
  const diffSeconds = ed.getTime() - sd.getTime()  // 毫秒
  const diffDays = diffSeconds / DayMillisecond // 天
	
	console.log('diffDays=', diffDays);
  
  let yCount = 0; // 年计数
  let mCount = 0; // 月计数
  let dCount = 0; // 日计数
  let hCount = 0; // 时
  let fCount = 0; // 分
  let sCount = 0; // 秒
  
  if (diffDays >= 365) {
    // 计算年
    const y1 = d1.getFullYear();
    const y2 = d2.getFullYear();
    yCount = y2 - y1;  // x年
    
    sd.setFullYear(y2); // 年份统一
  }
  if (diffDays >= 30) {
    // 计算月
    const sd_month = sd.getMonth();
    let ed_month = ed.getMonth();
    if (ed.getDate() < sd.getDate()) {
      ed_month -= 1
    }
    console.log('sd_month=', sd_month);
    console.log('ed_month=', ed_month);
    if (ed_month >= sd_month) {
      mCount = ed_month - sd_month;
    } else {
      mCount = 12 - sd_month + ed_month
    }
		if (ed_month < 0) {
			sd.setMonth(0); // 月份统一
		} else {
			sd.setMonth(ed_month); // 月份统一
		}
  }
  
  // 计算天
	console.log('计算天:')
	console.log('ed=', ed, 'sd=', sd)
  const calcSeconds = ed.getTime() - sd.getTime()
  dCount = Math.floor(calcSeconds / DayMillisecond);
  let leaveMilliSecond = calcSeconds - dCount * DayMillisecond;
  
  // 计算小时
  hCount = Math.floor(leaveMilliSecond / (60 * 60 * 1000));
  
  // 计算分钟
  leaveMilliSecond = leaveMilliSecond - (hCount * 60 * 60 * 1000);
  fCount = Math.floor(leaveMilliSecond / (60 * 1000));
  
  // 计算秒
  leaveMilliSecond = leaveMilliSecond - (fCount * 60 * 1000);
  sCount = Math.floor(leaveMilliSecond / 1000);
  return {
    yCount,
    mCount,
    dCount,
    hCount,
    fCount,
    sCount
  }
}

export const calcDayCount = (d1, d2) => {
  const DayMillisecond = 24 * 60 * 60 * 1000;
  const sd = d1, ed = d2;
  const diffSeconds = (ed.getTime() - sd.getTime())  // 毫秒
  const diffDays = diffSeconds / DayMillisecond // 天
  return Math.floor(diffDays) + 1;
}


export const generateMonthDates = (year, month) => {
  let days = [];
  let firstDay = new Date(year, month, 1);
  let lastDay = new Date(year, month + 1, 0);
  while (firstDay <= lastDay) {
    const week = firstDay.getDay();
    days.push({
      year: year,
      month: month,
      date: firstDay.getDate(),
      week: week,
      weekIndex: week == 0 ? 6 : (week - 1)
    });
    firstDay.setDate(firstDay.getDate() + 1);
  }

  return days;
}

export const dateFormat = (date, t, zero) => {
  const y = date.getFullYear();
  let m = date.getMonth() + 1;
  let d = date.getDate();
  if (zero) {
    let sm = m.toString().padStart(2, '0')
    let sd = d.toString().padStart(2, '0')
    return t === 'ch' ? `${y}年${sm}月${sd}日` : `${y}${t}${sm}${t}${sd}`
  }
  return t === 'ch' ? `${y}年${m}月${d}日` : `${y}${t}${m}${t}${d}`
}


